﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GiMess.Net;
using GiMessCommons;
using System.Windows.Forms;

namespace GiMess
{
    public class NetMsgHandler
    {
        private DataFieldsReader dataReader;
        private DataFieldsWriter dataWriter;

        public void HandleMsg(NetMsg msg)
        {            
            Loger.LogLine(((NetServices)msg.Service).ToString(), Loger.ServerLogLevel.Verbose);
            switch ((NetServices)msg.Service)
            {
                case NetServices.LoginAccepted:
                    Loger.LogLine("Login accepted", Loger.ServerLogLevel.Verbose);
                    GiNetClient.Instance.SendListRequest();
                    FormsManager.MainForm.HideLoginScreen();
                    HandleAuth(msg);
                    break;
                case NetServices.LoginFailed:
                    Loger.LogLine("Login failed", Loger.ServerLogLevel.Verbose);
                    MessageBox.Show("Login failed", "Error");
                    break;
                case NetServices.ListSend:
                    InsertList(msg);
                    break;
                case NetServices.ListGroupsSend:
                    InsertGroups(msg);
                    break;
                case NetServices.SendMessage:
                    HandleIM(msg);
                    break;
                case NetServices.UpdateOnline:
                    FriendUpdateOnline(msg);
                    break;
                case NetServices.UpdateAvatar:
                    FriendUpdateAvatar(msg);
                    break;
                case NetServices.UpdateStatus:
                    FriendUpdateStatus(msg);
                    break;
                case NetServices.CreateAccSucces:
                    MessageBox.Show("Account created");
                    break;
                case NetServices.CreateAccFaied:
                    MessageBox.Show("Create account failed: " + msg.Data);
                    break;
                case NetServices.AddRequest:
                    FriendRequest(msg);
                    break;
                default:
                    break;
            }
        }

        private void FriendRequest(NetMsg msg)
        {
            FormsManager.CreateRequestForm(msg.Data);
        }

        private void FriendUpdateStatus(NetMsg msg)
        {
            dataReader = new DataFieldsReader(msg.Data);

            string username = dataReader.GetFieldData(Field.UpdateUserName);
            string status   = dataReader.GetFieldData(Field.UpdateStatus);

            FormsManager.MainForm.ListInvokeSetUserStatus(username, status);
        }

        private void InsertGroups(NetMsg msg)
        {
            int listCount;
            string group;

            dataReader = new DataFieldsReader(msg.Data);

            int.TryParse(dataReader.GetFieldData(Field.ListCount), out listCount);

            for (int i = 0; i < listCount; i++)
            {
                group = dataReader.GetFieldData(Field.ListUserGroup, i);
                FormsManager.MainForm.ListInvokeAddListGroup(group);
            }
        }

        private void FriendUpdateAvatar(NetMsg msg)
        {
            dataReader = new DataFieldsReader(msg.Data);

            string username   = dataReader.GetFieldData(Field.UpdateUserName);
            string avatarHash = dataReader.GetFieldData(Field.UpdateAvatar);

            FormsManager.MainForm.ListInvokeSetUserAvatar(username, avatarHash);
        }

        private void HandleIM(NetMsg msg)
        {
            string fromUsername;
            string fullName;
            string messageText;
            string messageFont;

            dataReader = new DataFieldsReader(msg.Data);

            fromUsername = dataReader.GetFieldData(Field.MessageUserOrigin);
            fullName     = dataReader.GetFieldData(Field.MessageUserFullName);
            messageText  = dataReader.GetFieldData(Field.MessageText);
            messageFont  = dataReader.GetFieldData(Field.MessageFont);

            FormsManager.HandleIM(fromUsername, fullName, messageText, messageFont);
        }

        private void InsertList(NetMsg msg)
        {
            int listCount;
            UserFriend friend;

            dataReader = new DataFieldsReader(msg.Data);

            int.TryParse(dataReader.GetFieldData(Field.ListCount), out listCount);

            for (int i = 0; i < listCount; i++)
            {
                friend = new UserFriend();
                friend.Group     = dataReader.GetFieldData(Field.ListUserGroup,    i);
                friend.FirstName = dataReader.GetFieldData(Field.ListUserFirstName,i);
                friend.LastName  = dataReader.GetFieldData(Field.ListUserLastName, i);
                friend.Username  = dataReader.GetFieldData(Field.ListUserName,     i);
                friend.Status    = dataReader.GetFieldData(Field.ListUserStatus,   i);
                friend.Avatar    = dataReader.GetFieldData(Field.ListUserAvatar,   i);
                friend.Online    = bool.Parse(dataReader.GetFieldData(Field.ListUserOnline, i));
                
                FormsManager.MainForm.ListInvokeAddListUser(friend);
            }
           
        }

        private void FriendUpdateOnline(NetMsg msg)
        {
            string username;
            bool   online;

            dataReader = new DataFieldsReader(msg.Data);

            username = dataReader.GetFieldData(Field.UpdateUserName);
            online   = bool.Parse(dataReader.GetFieldData(Field.UpdateOnline));

            FormsManager.MainForm.ListInvokeSetUserOnline(username, online);
        }

        private void HandleAuth(NetMsg msg)
        {
            dataReader = new DataFieldsReader(msg.Data);

            AuthInfo.FirstName = dataReader.GetFieldData(Field.AuthUserFristName);
            AuthInfo.LastName  = dataReader.GetFieldData(Field.AuthUserLastName);
            AuthInfo.Avatar    = dataReader.GetFieldData(Field.AuthUserAvatar);

            AvatarDownloader.SetOwnAvatar(AuthInfo.Avatar);
        }
    }
}
